package com.lg.system.role.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.lg.common.base.BaseController;
import com.lg.system.menu.entity.Menu;
import com.lg.system.role.entity.RoleMenu;
import com.lg.system.role.service.RoleMenuService;
import com.lg.utils.R;
import com.lg.utils.Tree;

/**
 * <p>
 * 角色与菜单对应关系 前端控制器
 * </p>
 *
 * @author dl
 * @since 2018-03-23
 */
@RestController
@RequestMapping("/role/roleMenu")
public class RoleMenuController extends BaseController<RoleMenu, RoleMenuService> {
	private String prefix="system/role/";
	@Autowired
	RoleMenuService roleMenuService;
	
	
	
	@GetMapping("/assign/{roleId}")
	public ModelAndView assign(@PathVariable("roleId") Long roleId){
		ModelAndView mView=new ModelAndView(prefix+"role_menu");
		mView.addObject("roleId", roleId);
		return mView;
	}
	
	
	@RequestMapping("/selectMenuByRole")
	public Tree<Menu> selectTreeList(Long roleId){
		Tree<Menu> menus=roleMenuService.selectMenuByRole(roleId,getUserId());
		return menus;
	}
	
	@RequestMapping("/assignMenu")
	public R assignMenu(Long roleId,@RequestParam(value="menuIds[]") List<Long> menuIds){
		if(null==menuIds||menuIds.isEmpty()||null==roleId){
			return R.error("参数异常!");
		}
		boolean f=roleMenuService.assignMenu(roleId,menuIds);
		if(f){
			clearAuthz(roleId);
			return R.ok();
		}
		else
		return R.error("操作失败");
	}
}
